parameters:
  archType: ''
  buildConfig: ''
  container: ''
  dependsOn: []
  isOfficialBuild: false
  osGroup: ''
  osSubgroup: ''
  platform: ''
  pool: ''
  runtimeVariant: ''
  testGroup: ''
  timeoutInMinutes: ''
  variables: {}

jobs:
- template: /eng/pipelines/common/templates/runtimes/xplat-job.yml
  parameters:
    archType: ${{ parameters.archType }}
    buildConfig: ${{ parameters.buildConfig }}
    container: ${{ parameters.container }}
    condition: and(succeeded(), ${{ parameters.isOfficialBuild }})
    helixType: 'build/product/'
    osGroup: ${{ parameters.osGroup }}
    osSubgroup: ${{ parameters.osSubgroup }}
    pool: ${{ parameters.pool }}
    runtimeVariant: ${{ parameters.runtimeVariant }}
    timeoutInMinutes: ${{ parameters.timeoutInMinutes }}

    dependsOn: ${{ parameters.dependsOn }}

    name: workloadsbuild
    displayName: Build Workloads

    variables:
    - name: officialBuildIdArg
      value: ''
    - ${{ if eq(parameters.isOfficialBuild, true) }}:
      - name: officialBuildIdArg
        value: '/p:OfficialBuildId=$(Build.BuildNumber)'
    - name: SignType
      value: $[ coalesce(variables.OfficialSignType, 'real') ]
    - name: workloadPackagesPath
      value: $(Build.SourcesDirectory)/artifacts/workloadPackages
    - name: workloadArtifactsPath
      value: $(Build.SourcesDirectory)/artifacts/workloads
    - ${{ parameters.variables }}

    steps:
    - task: DownloadPipelineArtifact@2
      inputs:
        artifact: 'IntermediateArtifacts'
        path: $(workloadPackagesPath)
        patterns: |
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.AOT.win-x64.Cross.android-*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.AOT.win-arm64.Cross.android-*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.AOT.win-x64.Cross.browser-wasm*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.AOT.win-arm64.Cross.browser-wasm*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.AOT.win-x64.Cross.wasi-wasm*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.AOT.win-arm64.Cross.wasi-wasm*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.Mono.android-*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.Mono.browser-wasm*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.Mono.multithread.browser-wasm*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.Mono.ios-*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.Mono.iossimulator-*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.Mono.maccatalyst-*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.Mono.multithread.browser-wasm*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.Mono.tvos-*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.Mono.tvossimulator-*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NETCore.App.Runtime.Mono.wasi-wasm*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NET.Workload.Mono.ToolChain.Current.Manifest*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NET.Workload.Mono.ToolChain.net6.Manifest*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NET.Workload.Mono.ToolChain.net7.Manifest*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NET.Workload.Mono.ToolChain.net8.Manifest*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NET.Runtime.MonoTargets.Sdk*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NET.Runtime.MonoAOTCompiler.Task*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NET.Runtime.WebAssembly.Sdk*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NET.Runtime.WebAssembly.Wasi*.nupkg
          IntermediateArtifacts/MonoRuntimePacks/Shipping/Microsoft.NET.Runtime.WebAssembly.Templates*.nupkg
          IntermediateArtifacts/windows_arm64/Shipping/Microsoft.NETCore.App.Runtime.win-arm64*.nupkg
          IntermediateArtifacts/windows_x64/Shipping/Microsoft.NETCore.App.Runtime.win-x64*.nupkg
          IntermediateArtifacts/windows_x86/Shipping/Microsoft.NETCore.App.Runtime.win-x86*.nupkg

    - task: CopyFiles@2
      displayName: Flatten packages
      inputs:
        sourceFolder: $(workloadPackagesPath)
        contents: '*/Shipping/*.nupkg'
        cleanTargetFolder: false
        targetFolder: $(workloadPackagesPath)
        flattenFolders: true

    - script: $(Build.SourcesDirectory)$(dir)build$(scriptExt) -subset mono.workloads -arch $(archType) -c $(buildConfig) $(officialBuildIdArg) -ci
      displayName: Build workload artifacts

    # Upload packages wrapping msis
    - template: /eng/pipelines/common/upload-intermediate-artifacts-step.yml
      parameters:
        name: workloads

    # Publish Logs
    - task: PublishPipelineArtifact@1
      displayName: Publish Logs
      inputs:
        targetPath: $(Build.SourcesDirectory)/artifacts/log
        artifactName: 'WorkloadLogs_Attempt$(System.JobAttempt)'
      continueOnError: true
      condition: always()

    # Delete wixpdb files before they are uploaded to artifacts
    - task: DeleteFiles@1
      displayName: Delete wixpdb's
      inputs:
        SourceFolder: $(workloadArtifactsPath)
        Contents: '*.wixpdb'

    # Upload artifacts to be used for generating VS components
    - template: /eng/pipelines/common/upload-intermediate-artifacts-step.yml
      parameters:
        name: workloads-vs
        publishPackagesCondition: false
        publishVSSetupCondition: true
